package com.ruoyi.canteen.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.canteen.constant.CanteenConfig;
import com.ruoyi.canteen.domain.CanteenCpOrder;
import com.ruoyi.canteen.domain.CanteenOrderPrinterLog;
import com.ruoyi.canteen.dto.FeiePrinterDto;
import com.ruoyi.canteen.mapper.CanteenCpOrderDetailMapper;
import com.ruoyi.canteen.mapper.CanteenCpOrderMapper;
import com.ruoyi.canteen.vo.CanteenOrderItemVo;
import com.ruoyi.canteen.vo.PrintOrderVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 飞鹅打印服务
 * @Author Ryan
 * @Date 2021/11/26 15:12
 * @Version 1.0
 */
@Service
public class FeiePrintService {

    private  String URL = "http://api.feieyun.cn/Api/Open/";
    private  String USER="413589030@qq.com";
    private  String UKEY="9pVYqaMAYmj9KvIB";

    private static final Logger log = LoggerFactory.getLogger(FeiePrintService.class);

    @Autowired
    private CanteenCpOrderDetailMapper canteenCpOrderDetailMapper;

    @Autowired
    private CanteenConfig canteenConfig;

    @Autowired
    private ICanteenCpOrderService canteenCpOrderService;

    @Autowired
    private ICanteenOrderPrinterLogService canteenOrderPrinterLogService;

    /**
     * 封装请求参数
     * @param apiName
     * @return
     */
    private MultiValueMap<String, String>  setParam(String apiName){
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        String STIME = String.valueOf(System.currentTimeMillis()/1000);
        map.add("user",USER);
        map.add("stime",STIME);
        map.add("sig", DigestUtils.sha1Hex(USER+UKEY+STIME));
        map.add("apiname",apiName);
        return map;
    }

    /**
     * 添加打印机
     * @param sn sn
     * @param key key
     * @param remark 说明
     * @return
     */
    public String  printerAddlist(String sn,String key,String remark){
        MultiValueMap<String, String>  map = setParam("Open_printerAddlist");
        map.add("printerContent",sn+"#"+key+"#"+remark);
        JSONObject object = getPost(map);
        JSONArray jsonArray = object.getJSONObject("data").getJSONArray("no");
        if(jsonArray==null||jsonArray.size()==0) {
            return "success";
        }
        return jsonArray.toJSONString();
    }

    public FeiePrinterDto printMsg(String sn, String content){
        return printMsg(sn, content,1);
    }

    public FeiePrinterDto printMsg(String sn,String content,Integer times){
        MultiValueMap<String, String>  map = setParam("Open_printMsg");
        map.add("sn",sn);
        map.add("content",content);
        map.add("times",times.toString());
        JSONObject object = getPost(map);
        return JSON.toJavaObject(object, FeiePrinterDto.class);
    }

    /**
     * 根据订单号打印订单
     * @param orderSn
     * @return
     */
    public Boolean printOrder(String orderSn){
        boolean flag = false;
        try{
            // 打印一张总单+一张外卖单+多张分单
            CanteenCpOrder cpOrder = canteenCpOrderService.selectCpOrderByNo(orderSn);
            executePrintOrderVos(cpOrder);
            flag = true;
        }catch (Exception ex){
            log.error(ex.toString());
        }
        return flag;
    }
    /**
     * 发送post 请求
     * @param map
     * @return
     */
    private JSONObject getPost(MultiValueMap<String, String>  map){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<JSONObject> requestEntity = new HttpEntity(map,headers);
        //发送请求
        RestTemplate remoteTemplate = new RestTemplate();
        ResponseEntity<String> forEntity = remoteTemplate.postForEntity(URL, requestEntity, String.class);
        return JSONObject.parseObject(forEntity.getBody());
    }

    //orderList为数组  b1代表名称列占用字节  b2单价列 b3数量列 b4金额列-->这里的字节数可按自己需求自由改写，详细往上看112行调用实际例子运用
//    public  String getContent(List<SshopOrderItem> sshopOrderItems,String time,Integer type,
//                              String totals, String remarks,String custInfo,int b1, int b2, int b3, int b4) {
//        String orderInfo = "<CB>*****消费</CB><BR>";
//        orderInfo +=type==1? " <C>(微信支付)</C><BR>":"<C>(积分支付)</C><BR>";
//        orderInfo += "名称           单价  数量 金额<BR>";
//        orderInfo += "--------------------------------<BR>";
//        for (int i = 0; i < sshopOrderItems.size(); i++) {
//            String title = sshopOrderItems.get(i).getProdName()+"-"+sshopOrderItems.get(i).getSkuName();
//            String price ="";
//            String total = "";
//
//            if(type==1) {
//                price = sshopOrderItems.get(i).getPrice().toString();
//                total=sshopOrderItems.get(i).getProductTotalAmount().toString();
//            }else{
//                price = sshopOrderItems.get(i).getScorePrice().toString();
//                total=sshopOrderItems.get(i).getProductTotalScore().toString();
//            }
//            String num = sshopOrderItems.get(i).getProdCount().toString();
//            price = addSpace(price, b2);
//            num = addSpace(num, b3);
//            total = addSpace(total, b4);
//            String otherStr =  " " + price + num+ " " + total;
//
//            int tl = 0;
//            try {
//                tl = title.getBytes("GBK").length;
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            int spaceNum = (tl / b1 + 1) * b1 - tl;
//            if (tl < b1) {
//                for (int k = 0; k < spaceNum; k++) {
//                    title += " ";
//                }
//                title += otherStr;
//            } else if (tl == b1) {
//                title += otherStr;
//            } else {
//                List<String> list = null;
//                if (isEn(title)) {
//                    list = getStrList(title, b1);
//                } else {
//                    list = getStrList(title, b1 / 2);
//                }
//                String s0 = titleAddSpace(list.get(0));
//                // 添加 单价 数量 总额
//                title = "<BR>"+s0 + otherStr + "<BR>";
//                String s = "";
//                for (int k = 1; k < list.size(); k++) {
//                    s += list.get(k);
//                }
//                try {
//                    s = getStringByEnter(b1, s);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                title += s;
//            }
//            orderInfo += title + "<BR><BR>";
//        }
//        orderInfo += "--------------------------------<BR>";
//        orderInfo += "合计：" + totals + "<BR>";
//        orderInfo += "下单时间：" + time + "<BR>";
//        orderInfo += "<CB>客户："+custInfo+"</CB><BR>";
//        if(StringUtils.isNotEmpty(remarks)) {
//            orderInfo += "<CB>备注：" + remarks + "</CB><BR>";
//        }
//        return orderInfo;
//    }
//
//

    /**
     * 获取总单拼接字符串
     * @param totalOrderVo 总单数据对象
     * @param b1 b1代表名称列占用字节
     * @param b2 b2单价列
     * @param b3 b3数量列
     * @param b4 b4金额列
     * @return
     */
    public String getTotalOrder(PrintOrderVo totalOrderVo,int b1, int b2, int b3, int b4){
        String orderTitle = "<CB>" + totalOrderVo.getCanteenName() + "订单</CB><BR>";
        String orderSn = "<CB>订单号：</CB><C>" + totalOrderVo.getOrderSn() + "</C><BR>";
//        String orderTime = "<CB>订餐时间：</CB><C>" + totalOrderVo.getOrderTime() + "</C><BR>";
        String orderInfo = orderTitle + orderSn;
        orderInfo += "菜品名称         单价  数量 金额<BR>";
        orderInfo += "--------------------------------<BR>";
//        String orderItems = "";
//        double totals = 0.0;
        for(CanteenOrderItemVo itemVo : totalOrderVo.getOrderItemVos()){
            String title = itemVo.getName();
            String price = itemVo.getPrice().toString();
            String num = itemVo.getNumber().toString();
            String total = "" + Double.valueOf(price) * Integer.parseInt(num);
            price = addSpace(price, b2);
            num = addSpace(num, b3);
            total = addSpace(total, b4);
            String otherStr =  " " + price + num+ " " + total;

            int tl = 0;
            try {
                tl = title.getBytes("GBK").length;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            int spaceNum = (tl / b1 + 1) * b1 - tl;
            if (tl < b1) {
                for (int k = 0; k < spaceNum; k++) {
                    title += " ";
                }
                title += otherStr;
            } else if (tl == b1) {
                title += otherStr;
            } else {
                List<String> list = null;
                if (isEn(title)) {
                    list = getStrList(title, b1);
                } else {
                    list = getStrList(title, b1 / 2);
                }
                String s0 = titleAddSpace(list.get(0));
                title = s0 + otherStr + "<BR>";// 添加 单价 数量 总额
                String s = "";
                for (int k = 1; k < list.size(); k++) {
                    s += list.get(k);
                }
                try {
                    s = getStringByEnter(b1, s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                title += s;
            }
            orderInfo += title + "<BR>";
//            String orderItem = itemVo.getName() +"      "+ itemVo.getNumber() + "  " + itemVo.getCpSum();
//            orderItems = orderItems + orderItem;
        }
        orderInfo += "--------------------------------<BR>";
        orderInfo += "整单备注：" + totalOrderVo.getOrderNotes() + "<BR>";
        orderInfo += "账单合计：" + totalOrderVo.getTotalPrice() + "元<BR>";
        orderInfo += "订餐时间：" + totalOrderVo.getOrderTime() + "<BR>";
        return orderInfo;
    }

    /**
     * 获取外卖单拼接字符串
     * @param wmOrderVo
     * @param b1 b1代表名称列占用字节
     * @param b2 b2单价列
     * @param b3 b3数量列
     * @param b4 b4金额列
     * @return
     */
    public String getWmOrder(PrintOrderVo wmOrderVo,int b1, int b2, int b3, int b4){
        String orderTitle = "<CB>" + wmOrderVo.getCanteenName() + "外卖订单</CB><BR>";
        String orderSeparator = "-----------订单信息-----------<BR>";
        String orderSn = "<C>订单号：" + wmOrderVo.getOrderSn() + "</C><BR>";
        String orderTime = "<C>订餐时间：" + wmOrderVo.getOrderTime() + "</C><BR>";
        String orderInfo = orderTitle + orderSeparator + orderSn + orderTime + orderSeparator;
        String contactName = "<CB>联系人：" + wmOrderVo.getContactName() + "</CB><BR>";
        String contactTel = "<C>联系电话：" + wmOrderVo.getContactTel() + "</C><BR>";
        String deliveryAddress = "<CB>送餐地址：" + wmOrderVo.getDeliveryAddress() + "</CB><BR>";
        String remark = "<C>备注：" + wmOrderVo.getOrderNotes() + "</C><BR>";
        String cpSeparator = "-----------菜品信息-----------<BR>";
        orderInfo += contactName + contactTel + deliveryAddress + remark + cpSeparator;
        orderInfo += "菜品名称            数量   总价  <BR>";
        orderInfo += "--------------------------------<BR>";
//        String orderItems = "";
//        for(CanteenOrderItemVo itemVo : wmOrderVo.getOrderItemVos()){
//            String orderItem = itemVo.getName() +"      "+ itemVo.getNumber() + "  " + itemVo.getCpSum();
//            orderItems = orderItems + orderItem;
//        }
        for(CanteenOrderItemVo itemVo : wmOrderVo.getOrderItemVos()){
            String title = itemVo.getName();
            String price = itemVo.getPrice().toString();
            String num = itemVo.getNumber().toString();
            String total = "" + Double.valueOf(price) * Integer.parseInt(num);
//            totals += Double.parseDouble(total);
//            price = addSpace(price, b2);
            num = addSpace(num, b3);
            total = addSpace(total, b4);
//            String otherStr =  " " + price + num+ " " + total;
            String otherStr =  " "  + num + " " + total;

            int tl = 0;
            try {
                tl = title.getBytes("GBK").length;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            int spaceNum = (tl / b1 + 1) * b1 - tl;
            if (tl < b1) {
                for (int k = 0; k < spaceNum; k++) {
                    title += " ";
                }
                title += otherStr;
            } else if (tl == b1) {
                title += otherStr;
            } else {
                List<String> list = null;
                if (isEn(title)) {
                    list = getStrList(title, b1);
                } else {
                    list = getStrList(title, b1 / 2);
                }
                String s0 = titleAddSpace(list.get(0));
                title = s0 + otherStr + "<BR>";// 添加 单价 数量 总额
                String s = "";
                for (int k = 1; k < list.size(); k++) {
                    s += list.get(k);
                }
                try {
                    s = getStringByEnter(b1, s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                title += s;
            }
            orderInfo += title + "<BR>";
        }
        orderInfo += "--------------------------------<BR>";
        orderInfo += "账单合计：" + wmOrderVo.getTotalPrice() + "<BR>";
        return orderInfo;
    }

    /**
     * 获取分单拼接字符串
     * @param fdOrderVo
     * @param itemVo
     * @return
     */
    public String getFdOrder(PrintOrderVo fdOrderVo, CanteenOrderItemVo itemVo){
        String orderTitle = "<CB>" + fdOrderVo.getCanteenName() + "分单#" + fdOrderVo.getBuyerId() + "</CB><BR>";
        String orderSn = "<C>订单号：" + fdOrderVo.getOrderSn() + "</C><BR>";
        String orderTime = "<C>下单时间：" + fdOrderVo.getOrderTime() + "</C><BR>";
        String remark = "<C>备注：" + fdOrderVo.getOrderNotes() + "</C><BR>";
        String cpSeparator = "--------------------------------<BR>";
        String orderInfo = orderTitle + orderSn + orderTime + remark + cpSeparator;
        String orderItem = itemVo.getName() +"      "+ itemVo.getNumber() + "份";
        orderInfo += orderItem;
        orderInfo += "--------------------------------<BR>";
        return orderInfo;
    }

    /**
     * 实际执行打印操作
     * @param order
     */
    public void executePrintOrderVos(CanteenCpOrder order){
        List<CanteenOrderItemVo> itemVos = canteenCpOrderDetailMapper.selectOrderDetailListByOrderId(order.getId());
        Map<String, String> mdPrinters = canteenConfig.getMdPrinter();
        Map<String, String> mdPrinterCount = canteenConfig.getMdPrinterCount();
        Map<Integer, String> mdNames = canteenConfig.getMdName();

        Integer zd_times = 1;
        Integer wm_times = 1;
        Integer fd_times = 1;
        if(StringUtils.isNotEmpty(mdPrinterCount.get(order.getMdId().toString() + "-ZD"))){
            zd_times = Integer.valueOf(mdPrinterCount.get(order.getMdId().toString() + "-ZD").trim());
        }
        if(StringUtils.isNotEmpty(mdPrinterCount.get(order.getMdId().toString() + "-WM"))){
            wm_times = Integer.valueOf(mdPrinterCount.get(order.getMdId().toString() + "-WM").trim());
        }
        if(StringUtils.isNotEmpty(mdPrinterCount.get(order.getMdId().toString() + "-FD"))){
            fd_times = Integer.valueOf(mdPrinterCount.get(order.getMdId().toString() + "-FD").trim());
        }
        String mdName = mdNames.get(order.getMdId());
        // 总单信息
        PrintOrderVo totalOrderVo = new PrintOrderVo();
        totalOrderVo.setPrinterSn(mdPrinters.get(order.getMdId().toString() + "-ZD"));
        totalOrderVo.setCanteenName(canteenConfig.getMdName().get(order.getMdId().intValue()));
        totalOrderVo.setOrderNotes(order.getOrderNotes());
        totalOrderVo.setOrderSn(order.getOrderNo());
        totalOrderVo.setOrderTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", order.getCreateTime()));
        totalOrderVo.setOrderItemVos(itemVos);
        totalOrderVo.setPrintType("ZD");
        totalOrderVo.setTotalPrice(order.getTotalPrice().toString());
        if(StringUtils.isNotEmpty(totalOrderVo.getPrinterSn())){
            FeiePrinterDto totalDto
                    = printMsg(totalOrderVo.getPrinterSn(), getTotalOrder(totalOrderVo,16, 6, 3, 6), zd_times);
            if(!totalDto.getRet().equals("0")){
                // TODO 打印失败，写入日志表
            }
            CanteenOrderPrinterLog printerLog = new CanteenOrderPrinterLog();
            printerLog.setPrinterName(mdName);
            printerLog.setPrinterType(totalOrderVo.getPrintType());
            printerLog.setOrderSn(totalOrderVo.getOrderSn());
            printerLog.setPrinterSn(totalOrderVo.getPrinterSn());
            printerLog.setReturnCode(totalDto.getRet());
            printerLog.setReturnMsg(totalDto.getMsg());
            printerLog.setReturnJson(totalDto.toString());
            canteenOrderPrinterLogService.insertCanteenOrderPrinterLog(printerLog);
        }
        // 外卖单信息(多个设备@分隔开)
        String wmPrints = mdPrinters.get(order.getMdId().toString() + "-WM");

        if(StringUtils.isNotEmpty(wmPrints)){
            if(wmPrints.contains("@")){
                // 多个设备
                String[] prints = wmPrints.split("@");
                for(String printSn : prints){
                    this.printWmOrder(order, itemVos, mdName, printSn, wm_times);
                }
            }else{
                // 单个设备
                this.printWmOrder(order, itemVos, mdName, wmPrints.trim(), wm_times);
            }
        }
//        PrintOrderVo wmOrderVo = new PrintOrderVo();
//        wmOrderVo.setPrinterSn(mdPrinters.get(order.getMdId().toString() + "-WM"));
//        wmOrderVo.setCanteenName(canteenConfig.getMdName().get(order.getMdId().intValue()));
//        wmOrderVo.setOrderNotes(order.getOrderNotes());
//        wmOrderVo.setOrderSn(order.getOrderNo());
//        wmOrderVo.setOrderTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", order.getCreateTime()));
//        // 订餐人信息读取
//        wmOrderVo.setContactName(order.getDeliverLinkMan());
//        wmOrderVo.setContactTel(order.getPhone());
//        wmOrderVo.setDeliveryAddress(order.getAddress());
//        wmOrderVo.setOrderItemVos(itemVos);
//        wmOrderVo.setPrintType("WM");
//        wmOrderVo.setTotalPrice(order.getTotalPrice().toString());
//        if(StringUtils.isNotEmpty(wmOrderVo.getPrinterSn())){
//            FeiePrinterDto wmDto = printMsg(wmOrderVo.getPrinterSn(), getWmOrder(wmOrderVo));
//            if(!wmDto.getRet().equals("0")){
//                // TODO 打印失败，写入日志表
//            }
//            CanteenOrderPrinterLog printerLog = new CanteenOrderPrinterLog();
//            printerLog.setPrinterName(mdName);
//            printerLog.setPrinterType(wmOrderVo.getPrintType());
//            printerLog.setOrderSn(wmOrderVo.getOrderSn());
//            printerLog.setPrinterSn(wmOrderVo.getPrinterSn());
//            printerLog.setReturnCode(wmDto.getRet());
//            printerLog.setReturnMsg(wmDto.getMsg());
//            printerLog.setReturnJson(wmDto.toString());
//            canteenOrderPrinterLogService.insertCanteenOrderPrinterLog(printerLog);
//        }
        // 分单信息
        PrintOrderVo fdOrderVo = new PrintOrderVo();
        fdOrderVo.setPrinterSn(mdPrinters.get(order.getMdId().toString() + "-FD"));
        fdOrderVo.setCanteenName(canteenConfig.getMdName().get(order.getMdId().intValue()));
        fdOrderVo.setOrderNotes(order.getOrderNotes());
        fdOrderVo.setOrderSn(order.getOrderNo());
        fdOrderVo.setOrderTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", order.getCreateTime()));
        fdOrderVo.setOrderItemVos(itemVos);
        fdOrderVo.setPrintType("FD");
        fdOrderVo.setBuyerId(order.getUserId().toString());
        if(StringUtils.isNotEmpty(fdOrderVo.getPrinterSn())){
            for(CanteenOrderItemVo itemVo : itemVos){
                FeiePrinterDto fdDto = printMsg(fdOrderVo.getPrinterSn(), getFdOrder(fdOrderVo, itemVo), fd_times);
                if(!fdDto.getRet().equals("0")){
                    // TODO 打印失败，写入日志表
                }
                CanteenOrderPrinterLog printerLog = new CanteenOrderPrinterLog();
                printerLog.setPrinterName(mdName);
                printerLog.setPrinterType(fdOrderVo.getPrintType());
                printerLog.setOrderSn(fdOrderVo.getOrderSn());
                printerLog.setPrinterSn(fdOrderVo.getPrinterSn());
                printerLog.setReturnCode(fdDto.getRet());
                printerLog.setReturnMsg(fdDto.getMsg());
                printerLog.setReturnJson(fdDto.toString());
                canteenOrderPrinterLogService.insertCanteenOrderPrinterLog(printerLog);
            }
        }
    }

    /**
     * 打印外卖订单
     * @param order
     * @param itemVos
     * @param mdName
     * @param printerSn
     * @param wm_times
     */
    private void printWmOrder(CanteenCpOrder order, List<CanteenOrderItemVo> itemVos,
                              String mdName, String printerSn, Integer wm_times){
        PrintOrderVo wmOrderVo = new PrintOrderVo();
        wmOrderVo.setPrinterSn(printerSn);
        wmOrderVo.setCanteenName(canteenConfig.getMdName().get(order.getMdId().intValue()));
        wmOrderVo.setOrderNotes(order.getOrderNotes());
        wmOrderVo.setOrderSn(order.getOrderNo());
        wmOrderVo.setOrderTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", order.getCreateTime()));
        // 订餐人信息读取
        wmOrderVo.setContactName(order.getDeliverLinkMan());
        wmOrderVo.setContactTel(order.getPhone());
        wmOrderVo.setDeliveryAddress(order.getAddress());
        wmOrderVo.setOrderItemVos(itemVos);
        wmOrderVo.setPrintType("WM");
        wmOrderVo.setTotalPrice(order.getTotalPrice().toString());
        if(StringUtils.isNotEmpty(wmOrderVo.getPrinterSn())){
            FeiePrinterDto wmDto = printMsg(wmOrderVo.getPrinterSn(),
                    getWmOrder(wmOrderVo, 16, 0, 3, 6), wm_times);
            if(!wmDto.getRet().equals("0")){
                // TODO 打印失败，写入日志表
            }
            CanteenOrderPrinterLog printerLog = new CanteenOrderPrinterLog();
            printerLog.setPrinterName(mdName);
            printerLog.setPrinterType(wmOrderVo.getPrintType());
            printerLog.setOrderSn(wmOrderVo.getOrderSn());
            printerLog.setPrinterSn(wmOrderVo.getPrinterSn());
            printerLog.setReturnCode(wmDto.getRet());
            printerLog.setReturnMsg(wmDto.getMsg());
            printerLog.setReturnJson(wmDto.toString());
            canteenOrderPrinterLogService.insertCanteenOrderPrinterLog(printerLog);
        }
    }

    private  String addSpace(String str, int size) {
        int len = str.length();
        if (len < size) {
            for (int i = 0; i < size - len; i++) {
                str += " ";
            }
        }
        return str;
    }

    private  Boolean isEn(String str) {
        Boolean b = false;
        try {
            b = str.getBytes("GBK").length == str.length();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return b;
    }

    private  List<String> getStrList(String inputString, int length) {
        int size = inputString.length() / length;
        if (inputString.length() % length != 0) {
            size += 1;
        }
        return getStrList(inputString, length, size);
    }

    private  List<String> getStrList(String inputString, int length, int size) {
        List<String> list = new ArrayList<String>();
        for (int index = 0; index < size; index++) {
            String childStr = substring(inputString, index * length, (index + 1) * length);
            list.add(childStr);
        }
        return list;
    }

    private static String substring(String str, int f, int t) {
        if (f > str.length()) {
            return null;
        }
        if (t > str.length()) {
            return str.substring(f, str.length());
        } else {
            return str.substring(f, t);
        }
    }
    private String titleAddSpace(String str) {
        int k=0;
        int b = 14;
        try {
            k = str.getBytes("GBK").length;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < b-k; i++) {
            str += " ";
        }
        return str;
    }
    private   String getStringByEnter(int length, String string) throws Exception {
        for (int i = 1; i <= string.length(); i++) {
            if (string.substring(0, i).getBytes("GBK").length > length) {
                return string.substring(0, i - 1) + "<BR>" + getStringByEnter(length, string.substring(i - 1));
            }
        }
        return string;
    }

}
